package com.iplant.basic.serviceImpl;

import java.util.*;
import java.util.stream.Collectors;

import com.iplant.base.po.EnumItem;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.po.enums.BaseEnum;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.po.excel.ExcelData;
import com.iplant.base.po.excel.ExcelLineData;
import com.iplant.base.po.excel.ExcelSheetData;
import com.iplant.base.utils.SessionConstants;
import com.iplant.base.utils.general.DesUtil;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.orm.ORMUtils;
import com.iplant.base.utils.sql.orm.PoService;
import com.iplant.basic.service.basicenum.bms.BMSDepartmentType;
import com.iplant.basic.service.po.bms.qft.BMSPersonQualification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.basic.service.BMSService;
import com.iplant.basic.service.po.bms.*;
import com.iplant.basic.serviceImpl.dao.bms.*;

@Service
public class BMSServiceImpl implements BMSService {

    public BMSServiceImpl() {
        // TODO Auto-generated constructor stub
    }

    private static Logger logger = LoggerFactory.getLogger(BMSServiceImpl.class);

    private static BMSService Instance = null;

    public static BMSService getInstance() {
        if (Instance == null)
            Instance = new BMSServiceImpl();

        return Instance;
    }

    //region PoServices
    private PoService<BMSPersonQualification> mBMSPersonQualificationPoService =
            ORMUtils.GetPoService(BMSPersonQualification.class);
    private PoService<BMSRole> mBMSRoleService =
            ORMUtils.GetPoService(BMSRole.class);

    private PoService<BMSEmployee> mBMSEmployeeService =
            ORMUtils.GetPoService(BMSEmployee.class);
    private PoService<BMSDepartment> mBMSDepartmentService =
            ORMUtils.GetPoService(BMSDepartment.class);
    //endregion

    //region 历史接口
    @Override
    public ServiceResult<BMSEmployee> BMS_LoginEmployee(String wLoginName, String wPassword, long wMAC) {
        ServiceResult<BMSEmployee> wResult = new ServiceResult<BMSEmployee>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSEmployeeDAO.getInstance().BMS_LoginEmployee(wLoginName, wPassword, wMAC, wErrorCode);

            // 更新在线时间
//            BMSEmployeeDAO.getInstance().BMS_UpdateOnline(wResult.Result, wErrorCode);

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<BMSEmployee> BMS_LoginEmployeeByUserName(String wUserName, String wPassword) {
        ServiceResult<BMSEmployee> wResult = new ServiceResult<BMSEmployee>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSEmployeeDAO.getInstance().BMS_LoginEmployeeByUserName(wUserName, wPassword, wErrorCode);

            // 更新在线时间
//            BMSEmployeeDAO.getInstance().BMS_UpdateOnline(wResult.Result, wErrorCode);

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<BMSEmployee> BMS_LoginEmployeeByToken(String wLoginName, String wToken) {
        ServiceResult<BMSEmployee> wResult = new ServiceResult<BMSEmployee>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSEmployeeDAO.getInstance().BMS_LoginEmployeeByToken(wLoginName, wToken, wErrorCode);
            // wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
//            BMSEmployeeDAO.getInstance().BMS_UpdateOnline(wResult.Result, wErrorCode);
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<BMSEmployee> BMS_LoginEmployeeByOne(String wLoginID) {
        ServiceResult<BMSEmployee> wResult = new ServiceResult<BMSEmployee>(new BMSEmployee());
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSEmployeeDAO.getInstance().BMS_QueryEmployeeByLoginID(BMSEmployee.SysAdmin, wLoginID,
                    wErrorCode);
//            BMSEmployeeDAO.getInstance().BMS_UpdateOnline(wResult.Result, wErrorCode);
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<BMSEmployee>> BMS_QueryEmployeeList(BMSEmployee wLoginUser, int wDepartmentID,
                                                                  int wPosition, int wDepartmentType,
                                                                  int wPositionType, int wActive,
                                                                  String wName, Pagination wPagination) {
        ServiceResult<List<BMSEmployee>> wResult = new ServiceResult<List<BMSEmployee>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSEmployeeDAO.getInstance().BMS_QueryEmployeeList(wLoginUser, wDepartmentID, wPosition,
                    wDepartmentType, wPositionType, wActive, wName, wPagination, wErrorCode);

            //处理角色名称
            Map<String, Object> wParams = new HashMap<String, Object>();
            List<BMSRole> wBMSRoleList = mBMSRoleService.SelectPOList(wLoginUser, wParams, Pagination.getMaxSize(),
                    wErrorCode);
            for (BMSEmployee wBMSEmployee : wResult.Result) {
                if (wBMSEmployee.RoleIDList == null || wBMSEmployee.RoleIDList.size() <= 0) {
                    continue;
                }

                wBMSEmployee.RoleName = StringUtils.Join(",",
                        wBMSRoleList.stream().filter(p -> wBMSEmployee.RoleIDList.stream().anyMatch(q -> q == p.ID)).map(p -> p.Name).collect(Collectors.toList()));
            }

            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<BMSEmployee>> BMS_QueryEmployeeList(BMSEmployee wLoginUser, List<Integer> wID) {
        ServiceResult<List<BMSEmployee>> wResult = new ServiceResult<List<BMSEmployee>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSEmployeeDAO.getInstance().BMS_QueryEmployeeList(wLoginUser, wID, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<BMSEmployee> BMS_QueryEmployeeByID(BMSEmployee wLoginUser, int wID) {
        ServiceResult<BMSEmployee> wResult = new ServiceResult<BMSEmployee>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSEmployeeDAO.getInstance().BMS_QueryEmployeeByID(wLoginUser, wID, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_AddEmployee(BMSEmployee wLoginUser, BMSEmployee wEmployee) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);

            BMSEmployeeDAO.getInstance().BMS_AddEmployee(wLoginUser, wEmployee, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
            if (StringUtils.isNotEmpty(wResult.FaultCode)) {
                return wResult;
            }

            List<BMSRole> wBMSRoleList = BMSRoleDAO.getInstance().BMS_GetRoleList(wLoginUser, null, "", wEmployee.ID, 1,
                    Pagination.getMaxSize(), wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
            if (StringUtils.isNotEmpty(wResult.FaultCode)) {
                return wResult;
            }

            if (wEmployee.RoleIDList == null) {
                wEmployee.RoleIDList = new ArrayList<Integer>();
            }
            wEmployee.RoleIDList.removeIf(p -> p <= 0);

            wEmployee.RoleIDList = wEmployee.RoleIDList.stream().distinct().collect(Collectors.toList());
            // 原来的角色
            List<Integer> wRoleIDList = wBMSRoleList.stream().map(p -> p.ID).distinct().collect(Collectors.toList());

            if (wRoleIDList.size() == wEmployee.RoleIDList.size()) {
                wRoleIDList.removeIf(p -> wEmployee.RoleIDList.contains(p));
                if (wRoleIDList.size() == 0) {
                    return wResult;
                }
            }
            List<BMSRoleItem> wBMSRoleItemList = new ArrayList<BMSRoleItem>();

            for (Integer wRoleID : wEmployee.RoleIDList) {
                wBMSRoleItemList.add(new BMSRoleItem(wEmployee.ID, wRoleID));
            }
            BMSRoleDAO.getInstance().BMS_SaveRoleUserListByUser(wLoginUser, wEmployee.ID, wBMSRoleItemList, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }

        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_SaveEmployee(BMSEmployee wLoginUser, BMSEmployee wEmployee) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wEmployee.EditorID = wLoginUser.ID;
            wEmployee.EditTime = Calendar.getInstance();
            wResult.Result = BMSEmployeeDAO.getInstance().BMS_SaveEmployee(wLoginUser, wEmployee, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

            if (StringUtils.isNotEmpty(wResult.FaultCode)) {
                return wResult;
            }

            List<BMSRole> wBMSRoleList = BMSRoleDAO.getInstance().BMS_GetRoleList(wLoginUser, null, "", wEmployee.ID, 1,
                    Pagination.getMaxSize(), wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
            if (StringUtils.isNotEmpty(wResult.FaultCode)) {
                return wResult;
            }

            if (wEmployee.RoleIDList == null) {
                wEmployee.RoleIDList = new ArrayList<Integer>();
            }
            wEmployee.RoleIDList.removeIf(p -> p <= 0);

            wEmployee.RoleIDList = wEmployee.RoleIDList.stream().distinct().collect(Collectors.toList());
            // 原来的角色
            List<Integer> wRoleIDList = wBMSRoleList.stream().map(p -> p.ID).distinct().collect(Collectors.toList());

            if (wRoleIDList.size() == wEmployee.RoleIDList.size()) {
                wRoleIDList.removeIf(p -> wEmployee.RoleIDList.contains(p));
                if (wRoleIDList.size() == 0) {
                    return wResult;
                }
            }
            List<BMSRoleItem> wBMSRoleItemList = new ArrayList<BMSRoleItem>();

            for (Integer wRoleID : wEmployee.RoleIDList) {
                wBMSRoleItemList.add(new BMSRoleItem(wEmployee.ID, wRoleID));
            }
            BMSRoleDAO.getInstance().BMS_SaveRoleUserListByUser(wLoginUser, wEmployee.ID, wBMSRoleItemList, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_SavePassword(BMSEmployee wLoginUser, BMSEmployee wEmployee) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSEmployeeDAO.getInstance().BMS_SavePassword(wLoginUser, wEmployee, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_DisableEmployee(BMSEmployee wLoginUser, BMSEmployee wEmployee) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSEmployeeDAO.getInstance().BMS_DisableEmployee(wLoginUser, wEmployee, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_DeleteEmployee(BMSEmployee wLoginUser, BMSEmployee wEmployee) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSEmployeeDAO.getInstance().BMS_DeleteEmployee(wLoginUser, wEmployee, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_ActiveEmployee(BMSEmployee wLoginUser, BMSEmployee wEmployee) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSEmployeeDAO.getInstance().BMS_ActiveEmployee(wLoginUser, wEmployee, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_ResetPassword(BMSEmployee wLoginUser, BMSEmployee wEmployee) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSEmployeeDAO.getInstance().BMS_ResetPassword(wLoginUser, wEmployee, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }

        return wResult;
    }

    @Override
    public ServiceResult<BMSRole> BMS_AddRole(BMSEmployee wLoginUser, BMSRole wRole) {
        ServiceResult<BMSRole> wResult = new ServiceResult<BMSRole>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSRoleDAO.getInstance().BMS_AddRole(wLoginUser, wRole, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<BMSRole>> BMS_GetRoleList(BMSEmployee wLoginUser, List<Integer> wDepartmentID,
                                                        String wName, int wUserID, int wActive,
                                                        Pagination wPagination) {
        ServiceResult<List<BMSRole>> wResult = new ServiceResult<List<BMSRole>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSRoleDAO.getInstance().BMS_GetRoleList(wLoginUser, wDepartmentID, wName, wUserID,
                    wActive, wPagination, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<BMSRole>> BMS_GetRoleList(BMSEmployee wLoginUser, int wDepartmentID, int wIsTop,
                                                        String wName, int wUserID, int wActive,
                                                        Pagination wPagination) {
        ServiceResult<List<BMSRole>> wResult = new ServiceResult<List<BMSRole>>();
        try {

            if (wIsTop != 1 || wDepartmentID <= 0) {

                return this.BMS_GetRoleList(wLoginUser,
                        wDepartmentID > 0 ? StringUtils.parseListArgs(wDepartmentID) : null, wName, wUserID, wActive,
                        wPagination);

            }

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            List<BMSDepartment> wDepartmentList = BMSDepartmentDAO.getInstance().BMS_QueryDepartmentListByID(wLoginUser,
                    wDepartmentID, "", wErrorCode);

            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
            if (wDepartmentList.size() > 0) {
                wResult.Result = BMSRoleDAO.getInstance().BMS_GetRoleList(wLoginUser,
                        wDepartmentList.stream().map(p -> p.ID).collect(Collectors.toList()), wName, wUserID, wActive,
                        wPagination, wErrorCode);
            } else {
                wResult.Result = new ArrayList<BMSRole>();
            }

            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_RemoveRole(BMSEmployee wLoginUser, int wID) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            BMSRoleDAO.getInstance().BMS_RemoveRole(wLoginUser, wID, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_SaveRole(BMSEmployee wLoginUser, BMSRole wRole) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSRoleDAO.getInstance().BMS_SaveRole(wLoginUser, wRole, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_DeleteRole(BMSEmployee wLoginUser, BMSRole wRole) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            BMSRoleDAO.getInstance().DeletePO(wLoginUser, wRole, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_SaveRoleRangeList(BMSEmployee wLoginUser, int wRoleID,
                                                        List<BMSRoleItem> wFunctionList) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSRoleDAO.getInstance().BMS_SaveRoleRangeList(wLoginUser, wRoleID, wFunctionList,
                    wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_SaveRoleFunctionList(BMSEmployee wLoginUser, int wRoleID,
                                                           List<BMSRoleItem> wFunctionList) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSRoleDAO.getInstance().BMS_SaveRoleFunctionList(wLoginUser, wRoleID, wFunctionList,
                    wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_SaveRoleUserList(BMSEmployee wLoginUser, int wRoleID,
                                                       List<BMSRoleItem> wUserList) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSRoleDAO.getInstance().BMS_SaveRoleUserList(wLoginUser, wRoleID, wUserList, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<BMSRoleItem>> BMS_QueryRangeListByRoleID(BMSEmployee wLoginUser, int wRoleID) {
        ServiceResult<List<BMSRoleItem>> wResult = new ServiceResult<List<BMSRoleItem>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSRoleDAO.getInstance().BMS_QueryRangeListByRoleID(wLoginUser, wRoleID, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<BMSRoleItem>> BMS_QueryRangeListByOperatorID(BMSEmployee wLoginUser, int wOperatorID,
                                                                           int wFunctionID) {
        ServiceResult<List<BMSRoleItem>> wResult = new ServiceResult<List<BMSRoleItem>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSRoleDAO.getInstance().BMS_QueryRangeList(wLoginUser, wOperatorID, wFunctionID,
                    wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<BMSRoleItem>> BMS_QueryFunctionListByRoleID(BMSEmployee wLoginUser, int wRoleID) {
        ServiceResult<List<BMSRoleItem>> wResult = new ServiceResult<List<BMSRoleItem>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSRoleDAO.getInstance().BMS_QueryFunctionListByRoleID(wLoginUser, wRoleID, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<BMSRoleItem>> BMS_GetFunctionListByLoginID(BMSEmployee wLoginUser, int wUserID) {
        ServiceResult<List<BMSRoleItem>> wResult = new ServiceResult<List<BMSRoleItem>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSRoleDAO.getInstance().BMS_QueryFunctionListByLoginID(wLoginUser, wUserID, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<BMSRoleItem>> BMS_QueryUserListByFunctionID(BMSEmployee wLoginUser, int wFunctionID) {
        ServiceResult<List<BMSRoleItem>> wResult = new ServiceResult<List<BMSRoleItem>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSRoleDAO.getInstance().BMS_QueryUserListByFunctionID(wLoginUser, wFunctionID,
                    wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<BMSRoleItem>> BMS_QueryUserListByRoleID(BMSEmployee wLoginUser, int wRoleID) {
        ServiceResult<List<BMSRoleItem>> wResult = new ServiceResult<List<BMSRoleItem>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSRoleDAO.getInstance().BMS_QueryUserListByRoleID(wLoginUser, wRoleID, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_DisableRole(BMSEmployee wLoginUser, int wID) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSRoleDAO.getInstance().BMS_DisableRole(wLoginUser, wID, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_ActiveRole(BMSEmployee wLoginUser, int wID) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSRoleDAO.getInstance().BMS_ActiveRole(wLoginUser, wID, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_CheckPower(BMSEmployee wLoginUser, int wUserID, int wFunctionID, int wRangTypeID,
                                                 int wObjectID) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSRoleDAO.getInstance().BMS_CheckPower(wLoginUser, wUserID, wFunctionID, wRangTypeID,
                    wObjectID, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }

        return wResult;
    }

    @Override
    public ServiceResult<BMSRoleItem> BMS_UpdateFunctionNodeTree(BMSEmployee wLoginUser, BMSRoleItem wBMSRoleItem) {
        ServiceResult<BMSRoleItem> wResult = new ServiceResult<BMSRoleItem>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            BMSRoleDAO.getInstance().BMS_UpdateFunctionNodeTree(wLoginUser, wBMSRoleItem, wErrorCode);
            wResult.Result = wBMSRoleItem;
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_ActiveFunctionNodeTree(BMSEmployee wLoginUser, List<Integer> wFunctionIDList,
                                                             int wActive) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            BMSRoleDAO.getInstance().BMS_ActiveFunctionNodeTree(wLoginUser, wFunctionIDList, wActive, wErrorCode);

            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_DeleteFunctionNodeTree(BMSEmployee wLoginUser, List<Integer> wFunctionIDList) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            BMSRoleDAO.getInstance().BMS_DeleteFunctionNodeTree(wLoginUser, wFunctionIDList, wErrorCode);

            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<BMSRoleItem>> BMS_GetFunctionNodeTree(BMSEmployee wLoginUser, int wActive) {
        ServiceResult<List<BMSRoleItem>> wResult = new ServiceResult<List<BMSRoleItem>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSRoleDAO.getInstance().BMS_GetFunctionNodeTree(wLoginUser, 0, wActive, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_AddPosition(BMSEmployee wLoginUser, BMSPosition wPosition) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            BMSPositionDAO.getInstance().BMS_AddPosition(wLoginUser, wPosition, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_SavePosition(BMSEmployee wLoginUser, BMSPosition wPosition) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            BMSPositionDAO.getInstance().BMS_SavePosition(wLoginUser, wPosition, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_ActivePositionByID(BMSEmployee wLoginUser, int wID) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSPositionDAO.getInstance().BMS_ActivePositionByID(wLoginUser, wID, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_DisablePositionByID(BMSEmployee wLoginUser, int wID) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSPositionDAO.getInstance().BMS_DisablePositionByID(wLoginUser, wID, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_DeletePosition(BMSEmployee wLoginUser, BMSPosition wBMSPosition) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            BMSPositionDAO.getInstance().BMS_DeletePosition(wLoginUser, wBMSPosition, wErrorCode);

            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<BMSPosition>> BMS_QueryPositionList(BMSEmployee wLoginUser, int wDepartmentID,
                                                                  String wName, int wActive, Pagination wPagination) {
        ServiceResult<List<BMSPosition>> wResult = new ServiceResult<List<BMSPosition>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSPositionDAO.getInstance().BMS_QueryPositionList(wLoginUser, wDepartmentID, wName,
                    wActive, wPagination, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<BMSPosition> BMS_QueryPositionByID(BMSEmployee wLoginUser, int wID, String wCode) {
        ServiceResult<BMSPosition> wResult = new ServiceResult<BMSPosition>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            BMSPositionDAO.getInstance().BMS_QueryPositionByID(wLoginUser, wID, wCode, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_AddDepartment(BMSEmployee wLoginUser, BMSDepartment wDepartment) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            BMSDepartmentDAO.getInstance().BMS_AddDepartment(wLoginUser, wDepartment, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_SaveDepartment(BMSEmployee wLoginUser, BMSDepartment wDepartment) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            BMSDepartmentDAO.getInstance().BMS_SaveDepartment(wLoginUser, wDepartment, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_ActiveDepartmentByID(BMSEmployee wLoginUser, int wID) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSDepartmentDAO.getInstance().BMS_ActiveDepartmentByID(wLoginUser, wID, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_DisableDepartmentByID(BMSEmployee wLoginUser, int wID) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSDepartmentDAO.getInstance().BMS_DisableDepartmentByID(wLoginUser, wID, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_DeleteDepartment(BMSEmployee wLoginUser, BMSDepartment wDepartment) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            BMSDepartmentDAO.getInstance().BMS_DeleteDepartment(wLoginUser, wDepartment, wErrorCode);

            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<BMSDepartment>> BMS_QueryDepartmentList(BMSEmployee wLoginUser, int wParentID,
                                                                      String wName, int wType, int wActive,
                                                                      Pagination wPagination) {
        ServiceResult<List<BMSDepartment>> wResult = new ServiceResult<List<BMSDepartment>>();
        try {

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSDepartmentDAO.getInstance().BMS_QueryDepartmentList(wLoginUser, wParentID, wName, wType,
                    wActive, wPagination, wErrorCode);

            //翻译部门负责人
            if (wResult.Result.size() > 0) {
                Map<String, Object> wParams = new HashMap<String, Object>();
                List<BMSEmployee> wBMSEmployeeList = mBMSEmployeeService.SelectPOList(wLoginUser, wParams,
                        Pagination.getMaxSize(), wErrorCode);
                for (BMSDepartment wBMSDepartment : wResult.Result) {
                    if (wBMSDepartment.ManagerID != null && wBMSDepartment.ManagerID.size() > 0) {
                        wBMSDepartment.ManagerName = StringUtils.Join(",",
                                wBMSEmployeeList.stream().filter(p -> wBMSDepartment.ManagerID.stream().
                                        anyMatch(q -> q == p.ID)).map(p -> p.Name).collect(Collectors.toList()));
                    }
                    //部门成员数量
                    wBMSDepartment.EmployeeCount =
                            (int) wBMSEmployeeList.stream().filter(p -> p.DepartmentID == wBMSDepartment.ID).count();
                }
            }


            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<BMSDepartment> BMS_QueryDepartmentByID(BMSEmployee wLoginUser, int wID, String wCode) {
        ServiceResult<BMSDepartment> wResult = new ServiceResult<BMSDepartment>();
        try {

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSDepartmentDAO.getInstance().BMS_QueryDepartmentByID(wLoginUser, wID, wCode, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }

        return wResult;
    }

    @Override
    public ServiceResult<List<BMSTeamItem>> BMS_GetTeamItemList(BMSEmployee wLoginUser, String wName, int wFactoryID,
                                                                int wWorkTypeID,
                                                                int wWorkshopID, int wTeamID, int wWorkAreaID,
                                                                int wStationID, String wStationCode, int wActive,
                                                                Pagination wPagination) {
        ServiceResult<List<BMSTeamItem>> wResult = new ServiceResult<List<BMSTeamItem>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSTeamItemDAO.getInstance().BMS_QueryTeamItemList(wLoginUser, wName, wFactoryID,
                    wWorkTypeID,
                    wWorkshopID, wTeamID, wWorkAreaID, wStationID, wStationCode, wActive, wPagination, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<BMSTeamItem> BMS_QueryTeamItem(BMSEmployee wLoginUser, int wID) {
        ServiceResult<BMSTeamItem> wResult = new ServiceResult<BMSTeamItem>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSTeamItemDAO.getInstance().BMS_QueryTeamItem(wLoginUser, wID, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_UpdateTeamItem(BMSEmployee wLoginUser, BMSTeamItem wBMSTeamItem) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            BMSTeamItemDAO.getInstance().BMS_UpdateTeamItem(wLoginUser, wBMSTeamItem, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_ActiveTeamItem(BMSEmployee wLoginUser, BMSTeamItem wBMSTeamItem, int wActive) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            BMSTeamItemDAO.getInstance().BMS_ActiveTeamItem(wLoginUser, wBMSTeamItem.ID, wActive, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_CheckPower(BMSEmployee wLoginUser, int wLoginID, String wPath, int wRangeID,
                                                 int wRangeType) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            wResult.Result = BMSRoleDAO.getInstance().BMS_CheckPower(wLoginUser, wLoginID, wPath, wRangeID, wRangeType,
                    wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<BMSTeam>> BMS_GetTeamList(BMSEmployee wLoginUser, String wName, int wFactoryID,
                                                        int wWorkshopID, int wDepartmentID, int wModuleID,
                                                        int wLeaderID, int wActive, Pagination wPagination) {
        ServiceResult<List<BMSTeam>> wResult = new ServiceResult<List<BMSTeam>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSTeamDAO.getInstance().BMS_GetTeamList(wLoginUser, wName, wFactoryID, wWorkshopID,
                    wDepartmentID, wModuleID, wLeaderID, wActive, wPagination, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<BMSTeam>> BMS_GetTeamList(BMSEmployee wLoginUser, List<Integer> wIDList) {
        ServiceResult<List<BMSTeam>> wResult = new ServiceResult<List<BMSTeam>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSTeamDAO.getInstance().BMS_GetTeamList(wLoginUser, wIDList, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<BMSTeam> BMS_GetTeam(BMSEmployee wLoginUser, int wID, String wCode) {
        ServiceResult<BMSTeam> wResult = new ServiceResult<BMSTeam>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSTeamDAO.getInstance().BMS_GetTeam(wLoginUser, wID, wCode, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_UpdateTeam(BMSEmployee wLoginUser, BMSTeam wBMSTeam) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            BMSTeamDAO.getInstance().BMS_UpdateTeam(wLoginUser, wBMSTeam, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_DeleteTeam(BMSEmployee wLoginUser, BMSTeam wBMSTeam) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            BMSTeamDAO.getInstance().BMS_DeleteTeam(wLoginUser, wBMSTeam, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_ActiveTeam(BMSEmployee wLoginUser, BMSTeam wBMSTeam, int wActive) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            BMSTeamDAO.getInstance().BMS_ActiveTeam(wLoginUser, wBMSTeam, wActive, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_DeleteTeamItem(BMSEmployee wLoginUser, BMSTeamItem wTeamItem) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            BMSTeamItemDAO.getInstance().BMS_DeleteTeamItem(wLoginUser, wTeamItem, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_SaveWorkType(BMSEmployee wLoginUser, BMSWorkType wWorkType) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            BMSWorkTypeDAO.getInstance().BMS_SaveWorkType(wLoginUser, wWorkType, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_DeleteWorkType(BMSEmployee wLoginUser, BMSWorkType wWorkType) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            BMSWorkTypeDAO.getInstance().BMS_DeleteWorkType(wLoginUser, wWorkType, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_ActiveWorkType(BMSEmployee wLoginUser, BMSWorkType wWorkType, int wActive) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            BMSWorkTypeDAO.getInstance().BMS_ActiveWorkType(wLoginUser, wWorkType, wActive, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<BMSWorkType>> BMS_QueryWorkTypeList(BMSEmployee wLoginUser, String wName, int wActive,
                                                                  Pagination wPagination) {
        ServiceResult<List<BMSWorkType>> wResult = new ServiceResult<List<BMSWorkType>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSWorkTypeDAO.getInstance().BMS_QueryWorkTypeList(wLoginUser, wName, wActive, wPagination,
                    wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<BMSWorkType> BMS_QueryWorkType(BMSEmployee wLoginUser, int wID, String wCode) {
        ServiceResult<BMSWorkType> wResult = new ServiceResult<BMSWorkType>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSWorkTypeDAO.getInstance().BMS_QueryWorkType(wLoginUser, wID, wCode, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_UpdateRegion(BMSEmployee wLoginUser, BMSRegion wRegion) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            BMSRegionDAO.getInstance().BMS_UpdateRegion(wLoginUser, wRegion, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_DeleteRegion(BMSEmployee wLoginUser, BMSRegion wRegion) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            BMSRegionDAO.getInstance().DeletePO(wLoginUser, wRegion, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_ActiveRegion(BMSEmployee wLoginUser, BMSRegion wRegion, int wActive) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            BMSRegionDAO.getInstance().BMS_ActiveRegion(wLoginUser, wRegion, wActive, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<BMSRegion> BMS_QueryRegion(BMSEmployee wLoginUser, int wID, String wCode) {
        ServiceResult<BMSRegion> wResult = new ServiceResult<BMSRegion>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSRegionDAO.getInstance().BMS_QueryRegion(wLoginUser, wID, wCode, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<BMSRegion>> BMS_QueryRegionList(BMSEmployee wLoginUser, String wName, int wTypeID,
                                                              int wParentID, String wFullName, int wActive,
                                                              Pagination wPagination) {
        ServiceResult<List<BMSRegion>> wResult = new ServiceResult<List<BMSRegion>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wErrorCode.set(0);
            wResult.Result = BMSRegionDAO.getInstance().BMS_QueryRegionList(wLoginUser, wName, wTypeID, wParentID,
                    wFullName, wActive, wPagination, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<EnumItem>> BMS_QueryDepartmentTypeList(BMSEmployee wLoginUser) {
        ServiceResult<List<EnumItem>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            wResult.Result = BaseEnum.getEnumList(BMSDepartmentType.values());

            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }
    //endregion

    //region 班组管理

    @Override
    public void InitClassInfo(BMSEmployee wLoginUser) {
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            List<BMSDepartment> bmsDepartments = BMSDepartmentDAO.getInstance().BMS_QueryDepartmentList(wLoginUser,
                    -1, "", 2, 1, Pagination.getMaxSize(), wErrorCode);

            PoService<BMSClassInfo> bmsClassInfoPoService = ORMUtils.GetPoService(BMSClassInfo.class);

            for (BMSDepartment wBMSDepartment : bmsDepartments) {
                //根据DepartmentID查询数据,若没有，则创建
                Map<String, Object> wParamMap = new HashMap<>();
                wParamMap.put("DepartmentID", wBMSDepartment.ID);
                BMSClassInfo wBMSClassInfo = bmsClassInfoPoService.SelectPO(wLoginUser,
                        wParamMap).Result;
                if (wBMSClassInfo.ID <= 0) {
                    wBMSClassInfo = new BMSClassInfo();
                    wBMSClassInfo.ID = 0;
                    wBMSClassInfo.DepartmentID = wBMSDepartment.ID;
                    wBMSClassInfo.ParentDepartmentID = wBMSDepartment.ParentID;
                    bmsClassInfoPoService.UpdatePo(wLoginUser, wBMSClassInfo, wErrorCode);
                }
            }
        } catch (Exception ex) {
            logger.error("InitClassInfo", ex);
        }
    }

    @Override
    public ServiceResult<Integer> BMS_ImportClassInfo(BMSEmployee wLoginUser, ExcelData wExcelData) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            if (wExcelData == null || wExcelData.sheetData == null || wExcelData.sheetData.size() <= 0) {
                wResult.FaultCode += "Excel格式错误，请联系系统管理员!";
                return wResult;
            }

            ExcelSheetData wExcelSheetData = wExcelData.sheetData.get(0);

            Map<String, Object> wParams = new HashMap<String, Object>();
            List<BMSDepartment> wBMSDepartmentList = mBMSDepartmentService.SelectPOList(wLoginUser, wParams,
                    Pagination.getNewMaxSize(),
                    wErrorCode);
            List<BMSEmployee> wBMSEmployeeList = mBMSEmployeeService.SelectPOList(wLoginUser, wParams,
                    Pagination.getNewMaxSize(),
                    wErrorCode);

            BMSDepartment wBMSDepartment;
            BMSClassInfo wBMSClassInfo;
            String wCol1;
            String wCol2;
            String wCol4;
            int wManagerID;
            for (int i = 0; i < wExcelSheetData.lineData.size(); i++) {
                if (i == 0) {
                    continue;
                }

                wCol1 = wExcelSheetData.lineData.get(i).colData.get(0);
                if (StringUtils.isEmpty(wCol1)) {
                    continue;
                }
                wCol2 = wExcelSheetData.lineData.get(i).colData.get(1);
                String wCol3 = wExcelSheetData.lineData.get(i).colData.get(2);
                wCol4 = wExcelSheetData.lineData.get(i).colData.get(3);
                String wCol5 = wExcelSheetData.lineData.get(i).colData.get(4);

                wBMSDepartment = new BMSDepartment();
                wBMSDepartment.Name = wCol1;
                wBMSDepartment.Code = wCol2;
                wBMSDepartment.ParentID = 229891;
                wBMSDepartment.Type = BMSDepartmentType.Class.getValue();
                wBMSDepartment.setUserInfo(wLoginUser);

                //保存部门
                int wAffectRows = mBMSDepartmentService.UpdatePo(wLoginUser, wBMSDepartment, wErrorCode);
                if (wAffectRows <= 0) {
                    wResult.FaultCode += ExceptionTypes.DBSQL.getLabel();
                    return wResult;
                }

                //创建班组
                wBMSClassInfo = new BMSClassInfo();
                wBMSClassInfo.setUserInfo(wLoginUser);
                wBMSClassInfo.Active = 0;
                wBMSClassInfo.Description = wCol5;
                wBMSClassInfo.DepartmentID = wBMSDepartment.ID;
                if (wBMSEmployeeList.stream().anyMatch(p -> p.Name.equals(wCol3))) {
                    wBMSClassInfo.MonitorIDs = StringUtils.Join(",",
                            wBMSEmployeeList.stream().filter(p -> p.Name.equals(wCol3)).map(p -> p.Name).distinct().collect(Collectors.toList()));
                }

                //处理班组成员
                if (StringUtils.isEmpty(wCol4)) {
                    continue;
                }

                String[] wSplits = wCol4.split("、");
                for (String wUserName : wSplits) {
                    if (wBMSEmployeeList.stream().noneMatch(p -> p.Name.equals(wUserName))) {
                        continue;
                    }

                    BMSEmployee wBMSEmployee =
                            wBMSEmployeeList.stream().filter(p -> p.Name.equals(wUserName)).findFirst().get();
                    wBMSEmployee.DepartmentID = wBMSDepartment.ID;
                    wBMSEmployee.setUserInfo(wLoginUser);
                    wAffectRows = mBMSEmployeeService.UpdatePo(wLoginUser, wBMSEmployee, wErrorCode);
                    if (wAffectRows <= 0) {
                        wResult.FaultCode += ExceptionTypes.DBSQL.getLabel();
                        return wResult;
                    }
                }
            }

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    //endregion

    //region 用户管理

    @Override
    public void BMS_UpdateLastOnlineTime(BMSEmployee wBMSEmployee) {
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            // 更新在线时间
            BMSEmployeeDAO.getInstance().BMS_UpdateOnline(wBMSEmployee, wErrorCode);
        } catch (Exception ex) {
            logger.error("BMS_UpdateLastOnlineTime", ex);
        }
    }

    @Override
    public ServiceResult<List<BMSEmployee>> BMS_QueryUserAllByLogin(BMSEmployee wLoginUser) {
        ServiceResult<List<BMSEmployee>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            Map<String, Object> wParams = new HashMap<String, Object>();
            wParams.put("DepartmentID", wLoginUser.DepartmentID);
            wParams.put("Active", 1);
            wResult.Result = mBMSEmployeeService.SelectPOList(wLoginUser, wParams, Pagination.getNewMaxSize(),
                    wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_ImportUser(BMSEmployee wLoginUser, ExcelData wExcelData) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            if (wExcelData == null || wExcelData.sheetData == null || wExcelData.sheetData.size() <= 0) {
                wResult.FaultCode += "Excel格式错误，请联系系统管理员!";
                return wResult;
            }

            ExcelSheetData wExcelSheetData = wExcelData.sheetData.get(0);

            Map<String, Object> wParams = new HashMap<String, Object>();
            List<BMSDepartment> wBMSDepartmentList = mBMSDepartmentService.SelectPOList(wLoginUser, wParams,
                    Pagination.getNewMaxSize(),
                    wErrorCode);
            List<BMSRole> wBMSRoleList = mBMSRoleService.SelectPOList(wLoginUser, wParams, Pagination.getNewMaxSize(),
                    wErrorCode);
            String wDefaultPassword = DesUtil.encrypt(BMSEmployee.defaultPassword, SessionConstants.appSecret);

            BMSEmployee wBMSEmployee;
            String wCol1;
            String wCol2;
            String wCol3;
            String wCol4;
            String wCol7;
            String wCol8;
            String wCol9;
            String wCol10;
            String wCol11;
            String wCol12;
            for (int i = 0; i < wExcelSheetData.lineData.size(); i++) {
                if (i == 0) {
                    continue;
                }

                wCol1 = wExcelSheetData.lineData.get(i).colData.get(0);
                if (StringUtils.isEmpty(wCol1)) {
                    continue;
                }
                wCol2 = wExcelSheetData.lineData.get(i).colData.get(1);
                wCol3 = wExcelSheetData.lineData.get(i).colData.get(2);
                wCol4 = wExcelSheetData.lineData.get(i).colData.get(3);
                String wCol5 = wExcelSheetData.lineData.get(i).colData.get(4);
                String wCol6 = wExcelSheetData.lineData.get(i).colData.get(5);
                wCol7 = wExcelSheetData.lineData.get(i).colData.get(6);
                wCol8 = wExcelSheetData.lineData.get(i).colData.get(7);
                wCol9 = wExcelSheetData.lineData.get(i).colData.get(8);
                wCol10 = wExcelSheetData.lineData.get(i).colData.get(9);
                wCol11 = wExcelSheetData.lineData.get(i).colData.get(10);
                wCol12 = wExcelSheetData.lineData.get(i).colData.get(11);

                wBMSEmployee = new BMSEmployee();
                wBMSEmployee.LoginID = wCol1;
                wBMSEmployee.LoginName = wCol1;
                wBMSEmployee.Name = wCol2;
                wBMSEmployee.Phone = wCol4;
                if (wBMSDepartmentList.stream().anyMatch(p -> p.Name.equals(wCol5))) {
                    wBMSEmployee.DepartmentID =
                            wBMSDepartmentList.stream().filter(p -> p.Name.equals(wCol5)).findFirst().get().ID;
                }
                wBMSEmployee.Email = wCol11;
                wBMSEmployee.WeiXin = wCol12;
                wBMSEmployee.Password = wDefaultPassword;

                if (wBMSRoleList.stream().anyMatch(p -> p.Name.equals(wCol6))) {
                    wBMSEmployee.RoleIDList =
                            wBMSRoleList.stream().filter(p -> p.Name.equals(wCol6)).map(p -> p.ID).collect(Collectors.toList());
                }

                wBMSEmployee.setUserInfo(wLoginUser);
                int wAffectRows = mBMSEmployeeService.UpdatePo(wLoginUser, wBMSEmployee, wErrorCode);

                if (wAffectRows <= 0) {
                    wResult.FaultCode += ExceptionTypes.DBSQL.getLabel();
                    return wResult;
                }

                //处理角色
                if (wBMSEmployee.RoleIDList != null && wBMSEmployee.RoleIDList.size() > 0) {
                    List<BMSRoleItem> wBMSRoleItemList = new ArrayList<BMSRoleItem>();

                    for (Integer wRoleID : wBMSEmployee.RoleIDList) {
                        wBMSRoleItemList.add(new BMSRoleItem(wBMSEmployee.ID, wRoleID));
                    }
                    BMSRoleDAO.getInstance().BMS_SaveRoleUserListByUser(wLoginUser, wBMSEmployee.ID, wBMSRoleItemList,
                            wErrorCode);
                }
            }

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_PasswordModifyNew(BMSEmployee wLoginUser, String wUserAcc, String wPassword,
                                                        String wOldPassWord) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            //①查询用户
            Map<String, Object> wParams = new HashMap<String, Object>();
            wParams.put("ID", StringUtils.parseInt(wUserAcc));
            BMSEmployee wBMSEmployee = mBMSEmployeeService.SelectPO(wLoginUser, wParams, wErrorCode);
            if (wBMSEmployee == null || wBMSEmployee.ID <= 0) {
                wResult.FaultCode += wUserAcc + "用户不存在!";
                return wResult;
            }
            //②验证老密码是否正确
            String wOldEncypt = DesUtil.encrypt(wOldPassWord, SessionConstants.appSecret);
            if (!wOldEncypt.equals(wBMSEmployee.Password)) {
                wResult.FaultCode += "老密码不正确!";
                return wResult;
            }
            //②验证新老密码不能相同
            String wNewEncypt = DesUtil.encrypt(wPassword, SessionConstants.appSecret);
            if (wNewEncypt.equals(wOldEncypt)) {
                wResult.FaultCode += "新老密码不能相同!";
                return wResult;
            }
            //③加密新密码
            wBMSEmployee.Password = wNewEncypt;
            //④修改用户密码
            int wAffectRows = mBMSEmployeeService.UpdatePo(wLoginUser, wBMSEmployee, wErrorCode);
            if (wAffectRows <= 0) {
                wResult.FaultCode += "密码修改失败，请联系系统管理员!";
                return wResult;
            }

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_ImportUserByClassInfo(BMSEmployee wLoginUser, ExcelData wExcelData) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            if (wExcelData == null || wExcelData.sheetData == null || wExcelData.sheetData.size() <= 0) {
                wResult.FaultCode += "导入失败：Excel格式错误!!!";
                return wResult;
            }

            ExcelSheetData wExcelSheetData = wExcelData.sheetData.get(0);
            if (wExcelSheetData.lineData == null || wExcelSheetData.lineData.size() <= 1) {
                wResult.FaultCode += "导入失败：Excel无内容!!!";
                return wResult;
            }

            //①部门信息
            Map<String, Object> wParams = new HashMap<String, Object>();
            List<BMSDepartment> wBMSDepartmentList = mBMSDepartmentService.SelectPOList(wLoginUser, wParams,
                    Pagination.getNewMaxSize(),
                    wErrorCode);
            if (wBMSDepartmentList == null || wBMSDepartmentList.size() <= 0) {
                wResult.FaultCode += "导入失败：未获取到组织架构信息!!!";
                return wResult;
            }
            Map<String, Integer> wNameIDDepartmentMap =
                    wBMSDepartmentList.stream().collect(Collectors.toMap(BMSDepartment::getName
                            , BMSDepartment::getID));

            //②用户信息
            List<BMSEmployee> wBMSEmployeeList = mBMSEmployeeService.SelectPOList(wLoginUser, wParams,
                    Pagination.getNewMaxSize(),
                    wErrorCode);
            if (wBMSEmployeeList == null || wBMSEmployeeList.size() <= 0) {
                wResult.FaultCode += "导入失败：未获取到用户信息!!!";
                return wResult;
            }

            List<BMSEmployee> wUserList = new ArrayList<>();
            BMSEmployee wBMSEmployee;
            for (int i = 0; i < wExcelSheetData.lineData.size(); i++) {
                if (i == 0) {
                    continue;
                }

                ExcelLineData wExcelLineData = wExcelSheetData.lineData.get(i);

                //班组名称
                String wCol1 = wExcelLineData.colData.get(0);
                if (StringUtils.isEmpty(wCol1)) {
                    continue;
                }
                if (!wNameIDDepartmentMap.containsKey(wCol1)) {
                    wResult.FaultCode += StringUtils.Format("导入失败：第{0}行数据有误，班组【{1}】不存在!", i + 1, wCol1);
                    return wResult;
                }
                //班组长
                String wCol2 = wExcelLineData.colData.get(2);
                if (StringUtils.isNotEmpty(wCol2)) {
                    if (wBMSEmployeeList.stream().noneMatch(p -> p.Name.equals(wCol2))) {
                        //创建班组长
                        wBMSEmployee = new BMSEmployee();
                        wBMSEmployee.Name = wCol2;
                        wBMSEmployee.Active = 1;
                        wBMSEmployee.Password = "bWawWCEU5fc=";
                        wBMSEmployee.DepartmentID = wNameIDDepartmentMap.get(wCol1);
                        wBMSEmployee.setUserInfo(wLoginUser);
                        wUserList.add(wBMSEmployee);
                    }
                }
                //班组成员
                String wCol3 = wExcelLineData.colData.get(3);
                if (StringUtils.isNotEmpty(wCol3)) {
                    String[] split = wCol3.split("、");
                    for (String s : split) {
                        if (wBMSEmployeeList.stream().noneMatch(p -> p.Name.equals(s))) {
                            //创建班组长
                            wBMSEmployee = new BMSEmployee();
                            wBMSEmployee.Name = s;
                            wBMSEmployee.Active = 1;
                            wBMSEmployee.Password = "bWawWCEU5fc=";
                            wBMSEmployee.DepartmentID = wNameIDDepartmentMap.get(wCol1);
                            wBMSEmployee.setUserInfo(wLoginUser);
                            wUserList.add(wBMSEmployee);
                        }
                    }
                }
            }
            if (wUserList.size() > 0) {
                for (BMSEmployee bmsEmployee : wUserList) {
                    mBMSEmployeeService.UpdatePo(wLoginUser, bmsEmployee, wErrorCode);
                    if (bmsEmployee.ID > 0) {
                        bmsEmployee.LoginID = String.valueOf(bmsEmployee.ID);
                        bmsEmployee.LoginName = String.valueOf(bmsEmployee.ID);
                    }
                    mBMSEmployeeService.UpdatePo(wLoginUser, bmsEmployee, wErrorCode);
                }
            }

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error("MSS_ImportMaterial", e);
        }
        return wResult;
    }

    //endregion

    //region 部门管理

    @Override
    public ServiceResult<Integer> BMS_ImportDepartment(BMSEmployee wLoginUser, ExcelData wExcelData) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            if (wExcelData == null || wExcelData.sheetData == null || wExcelData.sheetData.size() <= 0) {
                wResult.FaultCode += "Excel格式错误，请联系系统管理员!";
                return wResult;
            }

            ExcelSheetData wExcelSheetData = wExcelData.sheetData.get(0);

            Map<String, Object> wParams = new HashMap<String, Object>();
            List<BMSDepartment> wBMSDepartmentList = mBMSDepartmentService.SelectPOList(wLoginUser, wParams,
                    Pagination.getNewMaxSize(),
                    wErrorCode);
            List<BMSEmployee> wBMSEmployeeList = mBMSEmployeeService.SelectPOList(wLoginUser, wParams,
                    Pagination.getNewMaxSize(),
                    wErrorCode);

            List<BMSDepartment> wNewBMSDepartmentList = new ArrayList<>();
            BMSDepartment wBMSDepartment;
            String wCol1;
            String wCol2;
            String wCol3;
            String wCol4;
            String wCol6;
            int wManagerID;
            for (int i = 0; i < wExcelSheetData.lineData.size(); i++) {
                if (i == 0) {
                    continue;
                }

                wCol1 = wExcelSheetData.lineData.get(i).colData.get(0);
                if (StringUtils.isEmpty(wCol1)) {
                    continue;
                }
                wCol2 = wExcelSheetData.lineData.get(i).colData.get(1);
                wCol3 = wExcelSheetData.lineData.get(i).colData.get(2);
                wCol4 = wExcelSheetData.lineData.get(i).colData.get(3);
                String wCol5 = wExcelSheetData.lineData.get(i).colData.get(4);
                wCol6 = wExcelSheetData.lineData.get(i).colData.get(5);

                wBMSDepartment = new BMSDepartment();
                wBMSDepartment.Name = wCol1;
                wBMSDepartment.Code = wCol2;
                wBMSDepartment.ParentName = wCol3;
                wBMSDepartment.Type = BMSDepartmentType.getEnumType(wCol4).getValue();
                if (wBMSEmployeeList.stream().anyMatch(p -> p.Name.equals(wCol5))) {
                    wManagerID = wBMSEmployeeList.stream().filter(p -> p.Name.equals(wCol5)).findFirst().get().ID;
                    wBMSDepartment.ManagerID = new ArrayList<>(Collections.singletonList(wManagerID));
                }
                wBMSDepartment.Description = wCol6;
                wNewBMSDepartmentList.add(wBMSDepartment);
            }

            //遍历保存数据
            int wAffectRows;
            for (BMSDepartment wBMSDepartmentItem : wNewBMSDepartmentList) {
                if (wBMSDepartmentList.stream().anyMatch(p -> p.Name.equals(wBMSDepartmentItem.ParentName))) {
                    wBMSDepartmentItem.ParentID =
                            wBMSDepartmentList.stream().filter(p -> p.Name.equals(wBMSDepartmentItem.ParentName)).findFirst().get().ID;
                }
                if (wBMSDepartmentItem.ParentID <= 0 && StringUtils.isNotEmpty(wBMSDepartmentItem.ParentName)) {
                    if (wNewBMSDepartmentList.stream().anyMatch(p -> p.Name.equals(wBMSDepartmentItem.ParentName))) {
                        wBMSDepartmentItem.ParentID =
                                wNewBMSDepartmentList.stream().filter(p -> p.Name.equals(wBMSDepartmentItem.ParentName)).findFirst().get().ID;
                    }
                }

                wBMSDepartmentItem.setUserInfo(wLoginUser);
                wAffectRows = mBMSDepartmentService.UpdatePo(wLoginUser, wBMSDepartmentItem, wErrorCode);
                if (wAffectRows <= 0) {
                    wResult.FaultCode += ExceptionTypes.DBSQL.getLabel();
                    return wResult;
                }
            }

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public Map<Integer, Integer> BMS_QueryDepartmentNumMap(BMSEmployee wLoginUser, List<Integer> wDepartmentIDList) {
        Map<Integer, Integer> wResult = new HashMap<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            wResult = BMSClassInfoDAO.getInstance().BMS_QueryDepartmentNumMap(wLoginUser, wDepartmentIDList,
                    wErrorCode);
        } catch (Exception e) {
            logger.error("BMS_QueryDepartmentNumMap" + e);
        }
        return wResult;
    }
    //endregion

    //region 权限管理
    @Override
    public ServiceResult<Integer> BMS_ImportRole(BMSEmployee wLoginUser, ExcelData wExcelData) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            if (wExcelData == null || wExcelData.sheetData == null || wExcelData.sheetData.size() <= 0) {
                wResult.FaultCode += "Excel格式错误，请联系系统管理员!";
                return wResult;
            }

            ExcelSheetData wExcelSheetData = wExcelData.sheetData.get(0);

            Map<String, Object> wParams = new HashMap<String, Object>();
            List<BMSDepartment> wBMSDepartmentList = mBMSDepartmentService.SelectPOList(wLoginUser, wParams,
                    Pagination.getNewMaxSize(),
                    wErrorCode);

            BMSRole wBMSRole;
            String wCol1;
            String wCol3;
            for (int i = 0; i < wExcelSheetData.lineData.size(); i++) {
                if (i == 0) {
                    continue;
                }

                wCol1 = wExcelSheetData.lineData.get(i).colData.get(0);
                if (StringUtils.isEmpty(wCol1)) {
                    continue;
                }
                String wCol2 = wExcelSheetData.lineData.get(i).colData.get(1);
                if (wBMSDepartmentList.stream().noneMatch(p -> p.Name.equals(wCol2))) {
                    wResult.FaultCode += StringUtils.Format("第【{0}】行数据错误，部门【{1}】不存在!", i + 1, wCol2);
                    return wResult;
                }

                wCol3 = wExcelSheetData.lineData.get(i).colData.get(2);


                wBMSRole = new BMSRole();
                wBMSRole.Name = wCol1;
                wBMSRole.DepartmentID =
                        wBMSDepartmentList.stream().filter(p -> p.Name.equals(wCol2)).findFirst().get().ID;
                wBMSRole.Description = wCol3;
                int wAffectRows = mBMSRoleService.UpdatePo(wLoginUser, wBMSRole, wErrorCode);
                if (wAffectRows <= 0) {
                    wResult.FaultCode += ExceptionTypes.DBSQL.getLabel();
                    return wResult;
                }
            }

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BMS_DeleteFunctionNodeTree_V1(BMSEmployee wBMSEmployee,
                                                                List<Integer> wFunctionIDList) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            if (wFunctionIDList == null || wFunctionIDList.size() <= 0) {
                return wResult;
            }

            BMSRoleDAO.getInstance().BMS_DeleteFunctionNodeTree_V1(wBMSEmployee, wFunctionIDList, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    //endregion
}
